//
//  CommunityViewController.m
//  Youhuiju
//
//  Created by yutenys on 16/7/29.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "CommunityViewController.h"


#import "CommunityTopView.h"//顶上视图
#import "CommunityTableViewCell.h"//tableViewCell




@interface CommunityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)CommunityTopView *topView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)UIButton *toTopButton;//回顶部

@end

@implementation CommunityViewController

static NSString *CommunityTableViewCellIden  = @"CommunityTableViewCellIden";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    

    [self load_UI];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark ------- load_UI
-(void)load_UI
{
    [self topView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"CommunityTableViewCell" bundle:nil] forCellReuseIdentifier:CommunityTableViewCellIden];
    
    _toTopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _toTopButton.backgroundColor = [UIColor clearColor];
    [_toTopButton setBackgroundImage:LoadImage(@"toTop") forState:UIControlStateNormal];
    [_toTopButton addTarget:self action:@selector(toTopAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_toTopButton];
    [_toTopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20 * FitW);
        make.bottom.equalTo(self.view.mas_bottom).offset(-100 * FitH);
        make.size.mas_equalTo(CGSizeMake(40 * FitW, 40 * FitH));
    }];
    _toTopButton.alpha = 0;
    
}

#pragma mark ---- tableView delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300 * FitH;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommunityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommunityTableViewCellIden forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    WeakObj(self);
    
    if (scrollView.contentOffset.y > 700 * FitH)
    {
        [UIView animateWithDuration:1 animations:^{
            weakself.toTopButton.alpha = 1;
        }];
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            weakself.toTopButton.alpha = 0;
        }];
    }
}


#pragma mark ------ private Method
-(void)toTopAction:(UIButton *)sender
{
    DLog(@"回顶部");
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
}


#pragma mark -------lazy loading
-(CommunityTopView *)topView
{
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[CommunityTopView alloc]init];
//        _topView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_topView];
        
        _topView.backButtonClickedHandler = ^{
        
            [weakself.navigationController popViewControllerAnimated:YES];
        };
        
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.right.equalTo(self.view.mas_right);
            make.height.mas_offset(44);
        }];
    }
    return _topView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
